package com.pgytesting.secondhandcar.service.impl;

import ch.qos.logback.classic.Logger;
import com.pgytesting.secondhandcar.dao.*;
import com.pgytesting.secondhandcar.model.*;
import com.pgytesting.secondhandcar.service.LoanRepaymentInformationService;
import com.pgytesting.secondhandcar.util.SessionUtil;
import com.pgytesting.secondhandcar.util.UtilMethod;
import com.pgytesting.secondhandcar.vo.Parameters;
import com.pgytesting.secondhandcar.vo.ResponseMessage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.beans.Transient;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

/**
 * 贷款还款信息表(LoanRepaymentInformation)表服务实现类
 *
 * @author makejava
 * @since 2019-08-23 16:44:03
 */
@Slf4j
@Service("loanRepaymentInformationService")
public class LoanRepaymentInformationServiceImpl implements LoanRepaymentInformationService {
    @Resource
    private LoanRepaymentInformationDao loanRepaymentInformationDao;

    @Resource
    private OrderNameDao orderNameDao;

    @Resource
    private RepaymentBillDao repaymentBillDao;

    @Resource
    private SalesCommissionSettlementDao salesCommissionSettlementDao;

    @Resource
    private ServiceInformationDao serviceInformationDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public LoanRepaymentInformation queryById(String id) {
        return this.loanRepaymentInformationDao.queryById(id);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<LoanRepaymentInformation> queryAllByLimit(int offset, int limit) {
        return this.loanRepaymentInformationDao.queryAllByLimit(offset, limit);
    }

    /**
     * 条件查询多条数据
     *
     * @return 对象列表
     */
    @Override
    public ResponseMessage queryAll() {
        // 创建接收还款信息表信息的对象
        List<LoanRepaymentInformation> resultList = new ArrayList<>();
        // 查询订单
        OrderName orderName = new OrderName();
        orderName.setUserNumber(SessionUtil.getSessionMcpUser().getUserNumber());
        List<OrderName> list = orderNameDao.queryAll(orderName);
        // 进行数据查询
        for (OrderName name : list) {
            resultList.add(loanRepaymentInformationDao.queryById(name.getLoanRepaymentInformationId()));
        }
        return ResponseMessage.successResponse(resultList, "查询数据成功");
    }

    /**
     * 新增数据
     *
     * @param loanRepaymentInformation 实例对象
     * @return 实例对象
     */
    @Override
    public LoanRepaymentInformation insert(LoanRepaymentInformation loanRepaymentInformation) {
        this.loanRepaymentInformationDao.insert(loanRepaymentInformation);
        return loanRepaymentInformation;
    }

    /**
     * 修改数据
     *
     * @return 实例对象
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public ResponseMessage update(String reviewInfID) {
        OrderName orderName = orderNameDao.queryReviewInfID(reviewInfID);
        String id = orderName.getLoanRepaymentInformationId();
        // 准备更新数据
        LoanRepaymentInformation loanRepaymentInformation = new LoanRepaymentInformation();
        loanRepaymentInformation.setId(id);
        loanRepaymentInformation.setStatus(0);
        loanRepaymentInformation.setRepaymentDate(Parameters.REPAYMENT_DATA);
        loanRepaymentInformation.setStartingTime(UtilMethod.unixLong());
        loanRepaymentInformation.setNumberRepayments(0);
        this.loanRepaymentInformationDao.update(loanRepaymentInformation);
        // 进行还款列表计算
        LoanRepaymentInformation loan = loanRepaymentInformationDao.queryById(loanRepaymentInformation.getId());
        RepaymentBill repaymentBill = new RepaymentBill();
        repaymentBill.setLoanRepaymentInformation(loan.getId());
        repaymentBill.setRepaymentAmount(UtilMethod.accountingInterest(loan.getLoanAmount(), loan.getLoanPeriod()));
        repaymentBill.setCreatedBy(SessionUtil.getSessionAdmin().getUserNumber());
        Integer countMonth = loan.getLoanPeriod() * 12;
        Long startTime = loan.getStartingTime();
        Integer year = Integer.valueOf(UtilMethod.getUnixToYear(startTime));
        Integer month = Integer.valueOf(UtilMethod.getUnixToMonth(startTime));
        for (int i = 0; i < countMonth; i++) {
        	month = month + 1;
            if (month == 13) {
                year += 1;
                month = 1;
            }
            Long repaymentTime = UtilMethod.getTimeToUnix(year +"-"+ month +"-"+ Parameters.REPAYMENT_DATA+" 00:00:00");
            repaymentBill.setRepaymentTime(repaymentTime);
            repaymentBill.setId(UtilMethod.getUniqueCode());
            repaymentBillDao.insert(repaymentBill);
        }
        // 进行订单中绑定服务信息查询
        ServiceInformation serviceInformation = serviceInformationDao.queryById(orderName.getServiceInformationId());
        // 进行销售佣金结算
        SalesCommissionSettlement settlement = new SalesCommissionSettlement();
        // 流水ID
        settlement.setId(UtilMethod.getUniqueCode());
        // 销售ID
        settlement.setMcpCarDealerInformationId(orderName.getSalesUserNumber());
        // 订单ID
        settlement.setOrderNameId(orderName.getId());
        // 审核ID
        settlement.setExamineInformationId(orderName.getReviewInformationId());
        // 更新用户
        settlement.setCreatedBy(SessionUtil.getSessionAdmin().getUserNumber());
        // 贷款金额
        settlement.setOrderAmount(loan.getLoanAmount());
        // 佣金比例
        settlement.setCommissionRate(Double.valueOf(serviceInformation.getCarDealerRebate()) / 100.00);
        // 收益金额
        settlement.setAmountIncome(Double.valueOf(settlement.getOrderAmount()) * settlement.getCommissionRate());
        // 插入是否默认为未结算
        settlement.setSettlementStatus("0");
        salesCommissionSettlementDao.insert(settlement);
        return ResponseMessage.successResponse("更新贷款信息成功");
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(String id) {
        return this.loanRepaymentInformationDao.deleteById(id) > 0;
    }
}